var _datagrid = $("#_datagrid");
var _queryForm = $("#_queryForm");
var _editForm = $("#_editForm");
var _editDialog = $("#_editDialog");

function _loadData() {
    _datagrid.datagrid('load', {params: JSON.stringify(_queryForm.serializeObject())});
}

function _add(url) {
    _editForm.form('clear');
    _editDialog.dialog({
        title: '添加',
        closed: false,
        cache: false,
        modal: true,
        iconCls: 'icon-add',
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    if (!_editForm.form('validate')) {
                        return;
                    }
                    $.ajax({
                        type: "POST",
                        url: url,
                        data: {params: JSON.stringify(_editForm.serializeObject())},
                        success: function () {
                            layer.msg("保存成功");
                            _loadData();
                        }
                    });
                    _editDialog.dialog('close');
                }
            },
            {
                text: '取消',
                handler: function () {
                    _editDialog.dialog('close');
                }
            }
        ]
    });
}

function _edit(editUrl, saveUrl) {
    _editForm.form('clear');
    var rows = _datagrid.datagrid("getSelections");
    if (rows.length != 1) {
        layer.msg("请选中一行数据");
        return;
    }

    $.ajax({
        type: "POST",
        url: editUrl,
        data: {id: rows[0]['id']},
        async: false,
        success: function (result) {
            var obj = JSON.parse(result.data);
            $("#_editForm :input").each(function () {
                if ("dict" == $(this).attr('qType')) {
                    $(this).combobox('setValue', obj[$(this).attr("comboname")]);
                }
                else if ("date" == $(this).attr('qType')) {
                    var datestr = obj[$(this).attr("comboname")];
                    if (datestr != "" && datestr != null) {
                        $(this).datebox('setValue', datestr.substr(0, 10));
                    }
                } else if ("time" == $(this).attr('qType')) {
                    $(this).datebox('setValue', obj[$(this).attr("comboname")]);
                }
                else if ("text" == $(this).attr('qType') || "textarea" == $(this).attr('qType')) {
                    $(this).val(obj[$(this).attr("name")]);
                }
            });
        }
    });

    _editDialog.dialog({
        title: '编辑',
        closed: false,
        iconCls: 'icon-edit',
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    if (!_editForm.form('validate')) {
                        return;
                    }
                    $.ajax({
                        type: "POST",
                        url: saveUrl,
                        data: {params: JSON.stringify(_editForm.serializeObject())},
                        async: false,
                        success: function () {
                            _loadData();
                        }
                    });
                    _editDialog.dialog('close');
                }
            },
            {
                text: '取消',
                handler: function () {
                    _editDialog.dialog('close');
                }
            }
        ]
    });
}

function _del(url) {
    var rows = _datagrid.datagrid("getSelections");
    if (rows.length == 0) {
        layer.msg("至少选择一行数据");
        return;
    }
    if (rows.length != 0) {
        layer.confirm('确定要删除数据？', {
            btn: ['确定', '取消']
        }, function () {
            var ids = "";
            for (var i = 0; i < rows.length; i++) {
                var row = rows[i];
                ids += row['id'] + ",";
            }
            $.ajax({
                type: "POST",
                url: url,
                data: {ids: ids.substr(0, ids.length - 1)},
                async: false,
                success: function () {
                    _loadData();
                }
            });
            layer.closeAll('dialog');
        }, function () {

        });
    }
}